import { Component,ViewChild} from '@angular/core';
import { NavController,Slides} from 'ionic-angular';

import {AppContentService} from "../../providers/AppContentService";
import {BANNER_PATH} from "../../providers/Constants"

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
	@ViewChild(Slides) slides: Slides;
  users: Array<{
    userface: string,
    username:string,
    pubtime:any,
    img:any,
    component: any,
    likeNum:any,
    commetNum:any,
    ago:any,
  }>;
  errorMessage: any;
  banners: string[];
  bannerPath:any;
  constructor(public navCtrl: NavController,
              private appContService : AppContentService,
  ) {
    this.users = [
      { userface: '2.jpg', username: '张小微',pubtime:'2017-7-21',img:'b1.jpg',component:'什么情人节快乐祝福语听多了，红包最实在',likeNum:13,commetNum:30,ago:11},
      { userface: '3.jpg', username: '张小微',pubtime:'2017-7-21',img:'b2.jpg',component:'你说你不喜欢月亮,你说你喜欢星星,可我喜欢月亮呀,因为今晚的月色真美！',likeNum:13,commetNum:30,ago:11},
    ];
  }

  ngOnInit(){//页面加载完成后自己调用
    this.slides.pager = true;
    //参照官网这个属性表示轮播图能够循环
    this.slides.loop = true;
    setInterval(()=>{
        //1000:表示速度，true表示能够触发事件
        this.slides.slideNext(1000,true);
    },2000);
  }
//解决自动播放的Slides调转到其他页面一定时间后（超过设置的播放时间），再回到此页面，自动播放失效
   //页面进入时启动自动播放
  // 进入的时候自动播放
  ionViewWillEnter() {
    this.bannerPath = BANNER_PATH;
    this.getBannerList();
    this.slides.startAutoplay();
    console.log("页面载入");
  }
// 离开的时候暂停
  ionViewWillLeave() {
    this.slides.stopAutoplay();
    console.log("页面退出");
  }

  //请求服务器资源
  getBannerList(){
     this.appContService.getBannerList().subscribe(
       res=>{
         console.log(res["msg"]);
         this.banners = res["data"];
       },
       error => this.errorMessage = <any>error);


  }

}
